package check

import (
	"auto/base"
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
	"reflect"
	"sort"
	"strings"
)

type Around struct {
	Distance interface{} `json:"distance"`
	Name     interface{} `json:"name"`
	Address  interface{} `json:"address"`
	Location interface{} `json:"location"`
}

func GetAround(location string, keyword string) (str string) {
	key := base.Config.Key
	radius := 3000
	url := fmt.Sprintf("https://restapi.amap.com/v3/place/around?key=%v&location=%v&keywords=%v&types=%v&radius=%v&offset=20&page=1&extensions=", key, location, keyword, keyword, radius)
	temp := make(map[string]interface{})
	err := httpGet(url, &temp)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	count := size(temp["pois"])
	if count == 0 {
		return
	}
	arounds := make([]Around, 0, count)
	for i := 0; i < count; i++ {
		_, ok := temp["pois"].([]interface{})[i].(map[string]interface{})["address"].(string)
		if !ok {
			continue
		}
		around := Around{}
		around.Distance = temp["pois"].([]interface{})[i].(map[string]interface{})["distance"]
		around.Name = temp["pois"].([]interface{})[i].(map[string]interface{})["name"]
		around.Address = temp["pois"].([]interface{})[i].(map[string]interface{})["address"]
		around.Location = temp["pois"].([]interface{})[i].(map[string]interface{})["location"]
		arounds = append(arounds, around)
	}
	b, err := json.Marshal(arounds)
	if err != nil {
		fmt.Println(err)
		return
	}
	str = string(b)
	return
}

func GetJieshao(location string, name string, address string) (str string) {
	gongjiao, _ := getGongjiaoInfo(location)
	ditie, _ := getDitieInfo(location)
	tingche, _ := getTingcheInfo(location, name)
	biaozhi, _ := getBiaozhiInfo(location)
	str = fmt.Sprintf("%v位于%v，%v%v%v%v", name, address, gongjiao, ditie, tingche, biaozhi)
	return
}

func getGongjiaoInfo(location string) (data string, err error) {
	key := base.Config.Key
	radius := 1000
	url := fmt.Sprintf("https://restapi.amap.com/v3/place/around?key=%v&location=%v&keywords=公交&types=公交&radius=%v&offset=50&page=1", key, location, radius)
	temp := make(map[string]interface{})
	err = httpGet(url, &temp)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	count := size(temp["pois"])
	if count == 0 {
		err = errors.New("数据为空")
		return
	}
	var build strings.Builder
	for i := 0; i < count; i++ {
		if temp["pois"].([]interface{})[i].(map[string]interface{})["address"] == nil {
			continue
		}
		build.WriteString(temp["pois"].([]interface{})[i].(map[string]interface{})["address"].(string) + ";")
	}
	str := build.String()
	str = strings.TrimRight(str, ";")
	arr := strings.Split(str, ";")
	arr = removeRepeatedElement(arr)
	count_gjx := len(arr)
	data = fmt.Sprintf("附近共有%v个公交站点%v个公交线路可以到达。", count, count_gjx)
	return
}

func getDitieInfo(location string) (data string, err error) {
	key := base.Config.Key
	radius := 1000
	url := fmt.Sprintf("https://restapi.amap.com/v3/place/around?key=%v&location=%v&keywords=地铁&types=地铁&radius=%v&offset=1&page=1", key, location, radius)
	temp := make(map[string]interface{})
	err = httpGet(url, &temp)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	count := size(temp["pois"])
	if count == 0 {
		err = errors.New("数据为空")
		return
	}
	name := temp["pois"].([]interface{})[0].(map[string]interface{})["name"].(string)
	distance := temp["pois"].([]interface{})[0].(map[string]interface{})["distance"].(string)
	data = fmt.Sprintf("除此之外您也可以乘坐地铁到%v下车步行%v米也可到达{$name}。", name, distance)
	return
}

func getTingcheInfo(location string, name string) (data string, err error) {
	key := base.Config.Key
	radius := 1000
	url := fmt.Sprintf("https://restapi.amap.com/v3/place/around?key=%v&location=%v&keywords=停车场&types=停车场&radius=%v&offset=1&page=1", key, location, radius)
	temp := make(map[string]interface{})
	err = httpGet(url, &temp)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	count := temp["count"].(string)
	if count == "0" {
		err = errors.New("数据为空")
		return
	}
	data = fmt.Sprintf("如果您是驾车的话，%v所在附近有%v个停车场可供您停车。", name, count)
	return
}

func getBiaozhiInfo(location string) (data string, err error) {
	key := base.Config.Key
	radius := 1000
	url := fmt.Sprintf("https://restapi.amap.com/v3/place/around?key=%v&location=%v&types=学校;医院;商场;写字楼&radius=%v&offset=20&page=1", key, location, radius)
	temp := make(map[string]interface{})
	err = httpGet(url, &temp)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	count := size(temp["pois"])
	if count == 0 {
		err = errors.New("数据为空")
		return
	}
	var build strings.Builder
	for i := 0; i < count; i++ {
		build.WriteString(temp["pois"].([]interface{})[i].(map[string]interface{})["name"].(string))
		build.WriteString("(" + temp["pois"].([]interface{})[i].(map[string]interface{})["distance"].(string) + "米)、")
	}
	str := build.String()
	str = strings.TrimRight(str, "、")
	data = fmt.Sprintf("附近的标志性建筑有%v等。", str)
	return
}

func size(temp interface{}) int {
	if reflect.TypeOf(temp).Kind() != reflect.Slice {
		return -1
	}
	ins := reflect.ValueOf(temp)
	return ins.Len()
}

func httpGet(url string, temp *map[string]interface{}) error {
	resp, err := http.Get(url)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	bs, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}
	json.Unmarshal([]byte(bs), temp)
	return err
}

func removeRepeatedElement(arr []string) (newArr []string) {
	newArr = make([]string, 0)
	sort.Strings(arr)
	for i := 0; i < len(arr); i++ {
		repeat := false
		for j := i + 1; j < len(arr); j++ {
			if arr[i] == arr[j] {
				repeat = true
				break
			}
		}
		if !repeat {
			newArr = append(newArr, arr[i])
		}
	}
	return
}
